// 导入外部模块
const { customRequire } = require('./utils.js');
const { Page } = customRequire('playwright');

class LoginPage {
    constructor(page) {
        this.page = page;
    }

    async GPOOpen(address) {
        // 锁定http
        // self.page.set_extra_http_headers({'ignore-ssl-errors': 'true'})
        // print(address)
        try {
            await this.page.goto(address, { 
                waitUntil: 'networkidle',
                timeout: 60000 
            });
        } catch (error) {
            // 网络连接错误处理
            if (error.message.includes('net::ERR_NAME_NOT_RESOLVED')) {
                throw new Error('网络连接失败：无法解析域名，请连接SGS VPN后重试');
            } else if (error.message.includes('net::ERR_INTERNET_DISCONNECTED')) {
                throw new Error('网络连接失败：网络已断开，请检查网络连接后重试');
            } else if (error.message.includes('net::ERR_CONNECTION_TIMED_OUT')) {
                throw new Error('网络连接失败：连接超时，请检查网络连接或稍后重试');
            } else if (error.message.includes('Timeout')) {
                throw new Error('页面加载超时，请检查网络连接或稍后重试');
            } else {
                throw new Error(`页面打开失败: ${error.message}`);
            }
        }
    }

    async enterUsername(username) {
        await this.page.fill('#username', username);
    }

    async enterPassword(password) {
        await this.page.fill('#password', password);
    }

    async clickLoginButton() {
        await this.page.click('#loginIn');
    }

    async singleSignOn() {
        await this.page.getByText('当前电脑用户直接登录').click();
    }
}

module.exports = {
    LoginPage
};
